package com.feihao.zhou.semaphoreDemo;

import java.util.concurrent.Semaphore;

public class MyThread implements Runnable{

    //1.获得管理员对象：只有两个通行证
    private Semaphore semaphore = new Semaphore(2);
    @Override
    public void run() {
        //获得通行证：
        try {
            semaphore.acquire();
            //执行任务：
            System.out.println(Thread.currentThread().getName()+"通行了");
            Thread.sleep(10);
            //归还行驶证：
            System.out.println("还了行驶证");
            semaphore.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
